// pages/order/order.js
import api from '../../utils/api.js'
import message from '../../utils/message';

var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderList:[],
    show_no_data_tip:false,
    status:0,
    page_index:1,
    no_more_data:false

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    console.log(options);
    this.getOrderList(options.status);
  },

  getOrderList:function( status ){

    var status_2 = 0;

    
    if(status==0){
      status_2 = this.data.status;
    }else{
      status_2 = status;
    }

    if(status ==undefined){
      status_2 = 0;
    }


    var params = {};
    params.status = status_2;
    message.show_loading();
    api.getOrderList( params, resp=>{
      message.hide_loading();
      if(resp.status==1){

        if(resp.data.length==0){
          this.setData({
            orderList:[],
            status:status_2,
            show_no_data_tip:true
          })
        }else{
          this.setData({
            orderList: resp.data,
            show_no_data_tip:false,
            status:status_2
          })
        }
      }else{
        wx.showToast({
          title: resp.message
        })
      }

    })
  },

  selectStatus:function(e){
    var status = e.currentTarget.dataset.status;
    console.log(status);

    this.setData({
      status:status,
      no_more_data:false
    })

    this.getOrderList( status )

  },

  getMore:function(){
    //加载当前分类下的更多数据
    var me = this;
    var page_index = this.data.page_index;
    page_index++;

    this.setData({
      page_index: page_index
    });

    var params ={}
    params.status = this.data.status;
    params.page_index = page_index;

    message.show_loading();
    api.getOrderList( params, resp=>{
      wx.hideLoading();
      if(resp.status==1){

        if(resp.data.length==0){
          this.setData({
            //orderList:[],
            //status:status,
            //show_no_data_tip:true
            no_more_data:true
          })
        }else{

          var order_list = this.data.orderList;

          var new_list = order_list.concat( resp.data );
          this.setData({
            orderList: new_list,
            show_no_data_tip:false,
            //status:status
          })
        }
      }else{
        wx.showToast({
          title: resp.message
        })
      }

    })

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  doOrderCancel:function( e ){
    var order_no = e.currentTarget.dataset.no;
    var me = this;
    api.doOrderCancel({order_no:order_no} , function(result){

      console.log(result);
      if(result.status==1){

        message.action_success("提示" , result.message ,"确定", function(){
          me.getOrderList();  
        })
        /*
        wx.showToast({
          title: result.message,
          duration:1000
        })
        me.getOrderList(); 
        */     
      }else{
        console.log(result)
      }

    })
  },
  doOrderPay:function(e){
    var order_no =  e.currentTarget.dataset.no;
    //var order_no = this.data.order_detail.order_no;
      /*
      wx.navigateTo({
        url: '/pages/pay/pay?no='+order_no
      })

      */

     //var order_no = this.data.order_no;
        
     var me = this;
     api.doPay({order_no:  order_no} , function(result){
         if(result.status==1){

             //console.log(result);
             me.appletPay( result.data , order_no )
         }else{
             console.log( result.message )
         }
     });
  },
  appletPay:function( payInfo , order_no ){
    //调用微信支付信息，勾起订单支付
    var me = this;
    wx.requestPayment({
      "timeStamp": payInfo.timeStamp.toString(),
      "nonceStr": payInfo.nonceStr,
      "package": payInfo.package,
      "signType": payInfo.signType,
      "paySign": payInfo.paySign,
      success:function( res ){
          console.log(res);
          //me.getOrderDetail(order_no);

          me.getOrderList(0)
          //console.log(order_no)
      },
      fail:function(res){
          console.log(res)
      }
    })

  },

  doOrderComplete:function(e){
    //确认订单完成

    var order_no = e.currentTarget.dataset.no

    var me = this;
    var params = {};
    params['order_no'] = order_no;
    api.doOrderComplete( params , function(result){
      console.log(result);
      if(result.status==1){
        wx.showModal({
          title: '提示',
          content: result.message,
          success:function( res ){
            if(res.confirm){
              //me.getOrderDetail(order_no);

              me.getOrderList( 0);
            }else if(  res.cancel){
              console.log("点击取消")
            }
          }
        })
      }
    })
  },
  doOrderRefund:function (e) {

    var order_no = e.currentTarget.dataset.no

    var me = this;
    var params = {};
    params['order_no'] = order_no;


    wx.navigateTo({
      url: '/pages/order/refund/refund?order_no='+order_no,
    })

    
  }
})